#    One Room And A Goblin Invasion
#    Copyright (C) 2014  Jannik Haberbosch
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.


# Python modules
import copy
import random

# 3rd party modules
import pygame
pygame.init()

# Project modules
from code import helpers

DOOR_IS_OPEN = 1
DOOR_IS_CLOSED = 2
DOOR_IS_OPENING = 3
DOOR_IS_CLOSING = 4


class Door( pygame.sprite.Sprite ):
	"""

	"""
	def __init__( self,
		imagefile,
		center_position,
		closed_door_angle,
		open_door_angle,
		door_degree_speed ):
		"""

		"""
		pygame.sprite.Sprite.__init__( self )

		self.imagefile = imagefile
		self.angle = closed_door_angle
		self.closed_door_angle = closed_door_angle
		self.open_door_angle = open_door_angle
		self.door_degree_speed = door_degree_speed
		self.image = pygame.transform.rotate(
			helpers.load_image(
				self.imagefile
			),
			self.angle
		)
		self.original_image = helpers.load_image(
			self.imagefile
		)
		self.center_position = center_position
		self.rect = self.image.get_rect(
			center = self.center_position
		)

		self.states = {
			DOOR_IS_OPEN: False,
			DOOR_IS_CLOSED: True,
			DOOR_IS_OPENING: False,
			DOOR_IS_CLOSING: False
		}
		self.state = DOOR_IS_CLOSED
		self.original_angle = copy.deepcopy(
			self.angle
		)

		self.let_goblin_walk_through = False
		self.create_goblin = False
		self.close_door_timer = None

		self.door_is_opening_max = 250

		self.counter = 0


	def update( self ):

		if self.counter == 1800:
			self.door_is_opening_max -= 50
		elif self.counter == 3600:
			self.door_is_opening_max -= 50
		elif self.counter == 5400:
			self.door_is_opening_max -= 50

		if self.counter < 5400:
			self.counter += 1
		else:
			self.counter = 10000

		if self.close_door_timer is not None:
			self.let_goblin_walking_through = False
			if self.close_door_timer.update() and self.state is not DOOR_IS_CLOSING:
				# Close the door now.
				self.state = DOOR_IS_CLOSING
				# Another timer gets installed if a goblin is entering.
				self.close_door_timer = None
		
		if self.state == DOOR_IS_CLOSED and random.randint( 1, self.door_is_opening_max ) == 1:
			# Open the door.
			self.state = DOOR_IS_OPENING

		if self.state == DOOR_IS_OPENING:
			self.opening_door()
			
		elif self.state == DOOR_IS_OPEN and self.close_door_timer == None:
			self.let_goblin_walk_through = True

		if self.state == DOOR_IS_CLOSING:
			self.closing_door()

		#self.door_is_opening_max -= 1			


	def opening_door( self ):
		if self.angle >= self.open_door_angle:
			self.state = DOOR_IS_OPEN
		else:
			self.angle += self.door_degree_speed
			self.image = pygame.transform.rotate(
				self.original_image,
				self.angle
			)
			self.rect = self.image.get_rect()
			self.rect.center = self.center_position


	def closing_door( self ):
		if self.angle <= self.closed_door_angle:
			self.state = DOOR_IS_CLOSED
		else:
			self.angle -= self.door_degree_speed
			self.image = pygame.transform.rotate(
				self.original_image,
				self.angle
			)
			self.rect = self.image.get_rect()
			self.rect.center = self.center_position		
